package com.csq.sso.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.manager.pojo.Account;
import com.csq.manager.pojo.Enterprise;
import com.csq.sso.service.LoginService;
import com.csq.sso.service.RegisterService;

@Controller
@RequestMapping("/sso")
public class RegisterContorller {

	@Autowired
	private RegisterService registerService;

	@Autowired
	private LoginService loginService;

	@Value("${TOKEN_KEY}")
	private String TOKEN_KEY;

	@RequestMapping(value = "/account/register", method = RequestMethod.POST)
	@ResponseBody
	public ReturnResult accountRigster(@RequestBody Account account,
			HttpServletRequest request) {

		System.out.println(account);

		String email = account.getEmail();
		if (email == null) {
			return ReturnResult.build(StatusCode.NULLUSERORPASSWORD,
					"用戶名或密碼為空!!");
		}

		ReturnResult result = loginService.checkAccountEmail(email);

		if (result.getState() == StatusCode.SUCCESS) {
			result = registerService.accountRigster(account);
		}
		return result;
	}

	@RequestMapping(value = "/company/register", method = RequestMethod.POST)
	@ResponseBody
	public ReturnResult enterpriseRigster(@RequestBody Enterprise enterprise,
			HttpServletRequest request) {

		System.out.println(enterprise);

		String email = enterprise.getWorkEmail();
		if (email == null) {
			return ReturnResult.build(StatusCode.NULLUSERORPASSWORD,
					"用戶名或密碼為空!!");
		}

		ReturnResult result = loginService.checkCompanyEmail(email);

		if (result.getState() == StatusCode.SUCCESS) {
			result = registerService.enterpriseRigster(enterprise);
		}
		return result;
	}

	@RequestMapping(value = "/account")
	public void testObject(@RequestBody Account account) {

		System.out.println(account);

	}

}
